#include "data_global.h"

#if 0
		long msgtype;//具体的消息类型
		消息类型的分配：
			1L: 		LED控制
			2L:			蜂鸣器控制
			3L:			四路LED灯模拟的数码管
			4L:			风扇
			5L:			温湿度最值设置
			6L-7L-8L-9L,用于个人的扩展
			10L: 		3G通信模块-GPRS 
#endif 

struct setEnv
{
	float temMAX;
	float temMIN;
	float humMAX;
	float humMIN;
	float illMAX;
	float illMIN;
};

extern int fd_zigbee;

extern int msgid;
extern key_t msg_key;

// 互斥锁
extern pthread_mutex_t mutex_client_request,
                mutex_refresh,
                mutex_sqlite,
                mutex_transfer,
                mutex_sms,
                mutex_zigbee,
                mutex_buzzer,
                mutex_led;

// 条件锁
extern pthread_cond_t  cond_client_request,
                cond_refresh,
                cond_sqlite,
                cond_transfer,
                cond_sms,
                cond_buzzer,
                cond_led;

extern struct From_to_send phone_NUM;
extern struct env_info_client_addr sm_all_env_info;
extern struct makeru_ctrl_data ctrl_data[MONITOR_NUM];

extern int set_com_config(int fd, int baud_rate, int data_bits, char parity, int stop_bits);
extern int open_port(char *com_port);
extern void USB_UART_Config(char* path, int baud_rate);

char zbuf[ZBUF_SIZ];
struct msg msgbuf;
void deal_msgs(void);

// 处理消息队列请求
void *pthread_client_request(void *arg)
{
    int i=0;
    struct setEnv new;

    printf("pthread_client_request\n");

    if((msg_key = ftok("/tmp", 'g')) < 0)
    {
        perror("ftok");
        exit(-1);
    }

    msgid = msgget(msg_key, IPC_CREAT|IPC_EXCL|0666);
    if(msgid == -1)
    {
        if(errno == EEXIST)
            msgid = msgget(msg_key, 0777);
        else
        {
            perror("msgget");
            exit(1);
        }
    }

    while(1)
    {
        bzero(&msgbuf, sizeof(msgbuf));
        printf("waiting from client request...\n");
        msgrcv(msgid, &msgbuf, sizeof(msgbuf) - sizeof(long), 1L, 0);
        printf("Get %ldL msg\n", msgbuf.msgtype);
        printf("text[0] = %#x\n", msgbuf.text[0]);

        if(msgbuf.msgtype != 10L && (msgbuf.text[0]&0xF0)>>4 != HOME_ID)
            continue;

        switch(msgbuf.msgtype)
        {
            case 1L:
                printf("hello led\n");
                pthread_mutex_lock(&mutex_led);
                ctrl_data[(msgbuf.text[0]&0xF0)>>4].ledstate = msgbuf.text[0]&0x0F;
                pthread_cond_signal(&cond_led);
                pthread_mutex_unlock(&mutex_led);
                break;
            case 2L:
                printf("hello beep\n");
                pthread_mutex_lock(&mutex_buzzer);
                ctrl_data[(msgbuf.text[0]&0xF0)>>4].beeperstate = msgbuf.text[0]&0x0F;
                pthread_cond_signal(&cond_buzzer);
                pthread_mutex_unlock(&mutex_buzzer);
                break;
            case 3L:
                printf("hello seg\n");
                break;
            case 4L:
                printf("hello fan\n");
                pthread_mutex_lock(&mutex_zigbee);
                sprintf(zbuf, "fan%d\n", msgbuf.text[0]&0x0F);
                printf("zbuf = %s\n", zbuf);
                if(fd_zigbee != -1) write(fd_zigbee, zbuf, sizeof(zbuf));
                pthread_mutex_unlock(&mutex_zigbee);
                break;
            case 5L:
                printf("hello env data\n");
                memcpy(&new, msgbuf.text+1, sizeof(new));
                printf("temMAX: %f\n", new.temMAX);
                printf("temMIN: %f\n", new.temMIN);
                printf("humMAX: %f\n", new.humMAX);
                printf("humMIN: %f\n", new.humMIN);
                // printf("illMAX: %d\n", new.illMAX);
                // printf("illMIN: %d\n", new.illMIN);
                
                pthread_mutex_lock(&mutex_transfer);
                ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[msgbuf.text[0]].zigbee_info.buffer)->tempMAX = new.temMAX;
                ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[msgbuf.text[0]].zigbee_info.buffer)->tempMIN = new.temMIN;
                ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[msgbuf.text[0]].zigbee_info.buffer)->humidityMAX = new.humMAX;
                ((struct makeru_zigbee_info*)sm_all_env_info.monitor_no[msgbuf.text[0]].zigbee_info.buffer)->humidityMIN = new.humMIN;
                pthread_mutex_unlock(&mutex_transfer);
                break;
            case 6L:
                break;
            case 7L:
                break;
            case 8L:
                break;
            case 9L:
                break;
            case 10L:
                // printf("deal_msgs:\n");
                deal_msgs();
                break;
            default:
                break;
        }
    }
}

void deal_msgs(void)
{
    memcpy(&phone_NUM, msgbuf.text, sizeof(phone_NUM));
    printf("recieve: %s\n", phone_NUM.recieve_phone);
    printf("center: %s\n", phone_NUM.center_phone);
    #if 0
        pthread_mutex_lock (&mutex_slinklist);
        sqlite_InsertLinknode (ENV_UPDATE, all_info_RT, sto_no, 0);//0,0鍒嗗埆鏄粨搴撳彿鍜岃揣鐗╃绫诲彿
        pthread_mutex_unlock (&mutex_slinklist);
        pthread_cond_signal (&cond_sqlite);
    #endif 
}




